package com.sy.easycms.dao.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 基本的增删改查
 * @author Luke Sun
 *
 */
public class DAO {

	public DAO() {
	}

	public ResultSet select(String sql) throws SQLException {
		Connection conn = SYConnection.getConnection();
		ResultSet rs = null;
		Statement stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		return rs;
	}

	public int update(String sql) throws SQLException
	{
		Connection conn = SYConnection.getConnection();
		Statement stmt = conn.createStatement();
		int i = stmt.executeUpdate(sql);
		close(conn);
		return i;
	}

	public int insert(String sql) throws SQLException {
		Connection conn = SYConnection.getConnection();
		Statement stmt = conn.createStatement();
		int i = stmt.executeUpdate(sql);
		close(conn);
		return i;
	}

	public int delete(String sql) throws SQLException {
		Connection conn = SYConnection.getConnection();
		Statement stmt = conn.createStatement();
		int i = stmt.executeUpdate(sql);
		close(conn);
		return i;
	}

	private void close(Connection conn) throws SQLException { 
		if(conn != null && !conn.isClosed())
		{
			conn.close();
		}
	}
}
